package com.chukun.reactor.multi;


import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author chukun@meituan.com
 * @version 1.0.0
 * @description 连接处理器
 * @create 2022/1/24 20:01
 **/
public class MultiAcceptor implements  Runnable {

    private Selector selector;

    private ServerSocketChannel serverSocketChannel;

    public MultiAcceptor(Selector selector, ServerSocketChannel serverSocketChannel) {
        this.selector = selector;
        this.serverSocketChannel = serverSocketChannel;
    }


    @Override
    public void run() {
        SocketChannel socketChannel;
        try {
            socketChannel = this.serverSocketChannel.accept();
            System.out.println(socketChannel.getRemoteAddress() + "received a connection....");
            socketChannel.configureBlocking(false);
            socketChannel.register(selector, SelectionKey.OP_READ, new MultiDispatchHandler(socketChannel));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
